﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MeinOS.ServiceController;
using Sys = Cosmos.System;

namespace MeinOS
{
    public class Kernel : Sys.Kernel
    {
        WriteManager writeManager;
        serviceController ServiceManager;
        protected override void BeforeRun()
        {
            bool debug = true;//set this to set wether or not debug data comes through.
            writeManager = new WriteManager(debug);
            ServiceManager = new serviceController(writeManager.getStdoutDelegate());
            Console.WriteLine("Cosmos booted successfully. Type a line of text to get it echoed back.");
        }

        protected override void Run()
        {
            Console.Write("Input: ");
            var input = Console.ReadLine();
            Console.Write("Text typed: ");
            Console.WriteLine(input);
        }
    }
    public class WriteManager
    {
        public WriteManager(bool debugMode)
        {
            DebugMode = debugMode;
        }
        bool DebugMode;
        string TextBuffer = "";
        bool newLineMode = false;
        public serviceController.stdOut getStdoutDelegate()
        {
            return stdout;
        }
        void stdout(string Text, bool WriteLine/*uses Console.Writeline if true, false uses Console.Write. stdout always exits setting this value to true.*/)
        {
            if (DebugMode)
            {
                TextBuffer = Text;
                newLineMode = WriteLine;
                main();
                TextBuffer = "";
                newLineMode = true;
            }
        }
        
        void main()
        {
            if (newLineMode)
            {
                Console.WriteLine(TextBuffer);
            }
            else if (!newLineMode)
            {
                Console.Write(TextBuffer);
            }
        }
    }
}
